package org.example.supermarketms.service;


import org.example.supermarketms.dao.HBaseDAO;
import org.example.supermarketms.model.OrderData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class DataService {

    @Autowired
    private HBaseDAO hBaseDAO;

    public Map<String, Double> monthlySales() throws IOException {
        List<OrderData> orders = hBaseDAO.scanAll();
        return orders.stream()
                .collect(Collectors.groupingBy(
                        this::extractYearMonth,
                        Collectors.summingDouble(OrderData::getActualAmount)
                ));
    }

    public Map<String, Integer> monthlyVegetableSales() throws IOException {
        List<OrderData> orders = hBaseDAO.scanAll();
        return orders.stream()
                .filter(o -> "蔬菜".equals(o.getCategory()))
                .collect(Collectors.groupingBy(
                        this::extractYearMonth,
                        Collectors.summingInt(OrderData::getQuantity)
                ));
    }

    public Map<String, Long> paymentMethodCount() throws IOException {
        List<OrderData> orders = hBaseDAO.scanAll();
        return orders.stream()
                .collect(Collectors.groupingBy(
                        OrderData::getPaymentMethod,
                        Collectors.counting()
                ));
    }

    public List<Map.Entry<String, Double>> topFiveDiscountedProducts() throws IOException {
        List<OrderData> orders = hBaseDAO.scanAll();
        Map<String, Double> discountMap = orders.stream()
                .collect(Collectors.groupingBy(
                        OrderData::getGoodsName,
                        Collectors.summingDouble(OrderData::getDiscount)
                ));
        return discountMap.entrySet().stream()
                .sorted(Map.Entry.<String, Double>comparingByValue().reversed())
                .limit(5)
                .toList();
    }

    public List<Map.Entry<String, Integer>> topFiveSoldProducts() throws IOException {
        List<OrderData> orders = hBaseDAO.scanAll();
        Map<String, Integer> quantityMap = orders.stream()
                .collect(Collectors.groupingBy(
                        OrderData::getGoodsName,
                        Collectors.summingInt(OrderData::getQuantity)
                ));
        return quantityMap.entrySet().stream()
                .sorted(Map.Entry.<String, Integer>comparingByValue().reversed())
                .limit(5)
                .toList();
    }

    public Map<String, Integer> categorySales() throws IOException {
        List<OrderData> orders = hBaseDAO.scanAll();
        return orders.stream()
                .collect(Collectors.groupingBy(
                        OrderData::getCategory,
                        Collectors.summingInt(OrderData::getQuantity)
                ));
    }

    private String extractYearMonth(OrderData data) {
        return data.getOrderTime().substring(0, 7); // "2022-03"
    }
}
